﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ZooSystem
{
    class Serialization
    {
        static private Serialization Database;

        public List<UserData> UsersList;
        public List<Animal> AnimalsList;

        private Serialization(){
            GetUsers();
            GetAnimals();
        }


        static public Serialization getDatabase() {
            if (Database == null)
                Database = new Serialization();
            return Database;

        }

        private void GetUsers() 
        {
            string s = Directory.GetCurrentDirectory();
            if (File.Exists(@"UsersDatabase.xml"))
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(List<UserData>));
                TextReader textReader = new StreamReader(@"UsersDatabase.xml");
                try
                {
                    UsersList = (List<UserData>)deserializer.Deserialize(textReader);
                }
                catch (Exception)
                {
                    UsersList = new List<UserData>();
                }
                finally
                {
                    textReader.Close();
                }
            }
            else
            {
                UsersList = new List<UserData>();
                AddUser(UserData.CreateAdmin());
            }
        }

        private void GetAnimals()
        {
            if (File.Exists(@"Animals.xml"))
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(List<Animal>));
                TextReader textReader = new StreamReader(@"Animals.xml");
                try
                {
                    AnimalsList = (List<Animal>)deserializer.Deserialize(textReader);
                }
                catch (Exception)
                {
                    AnimalsList = new List<Animal>();
                }
                finally
                {
                    textReader.Close();
                }
            }
            else
                AnimalsList = new List<Animal>();
        }

        public void AddUser(UserData NewUser)
        {
            UsersList.Add(NewUser);
            SaveUsers();
        }

        public void AddAnimal(Animal NewAnimal)
        {
            AnimalsList.Add(NewAnimal);
            SaveAnimals();
        }

        public void SaveUsers()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<UserData>));
            TextWriter textWriter = new StreamWriter(@"UsersDatabase.xml");
            serializer.Serialize(textWriter, UsersList);
            textWriter.Close();
        }

        public void SaveAnimals()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Animal>));
            TextWriter textWriter = new StreamWriter(@"Animals.xml");
            serializer.Serialize(textWriter, AnimalsList);
            textWriter.Close();
        }
    }
}